package com.tsunami.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsunami.auth.service.auth.IMenuService;
import com.tsunami.authority.dto.auth.MenuSaveDTO;
import com.tsunami.authority.dto.auth.MenuTreeDTO;
import com.tsunami.authority.dto.auth.MenuUpdateDTO;
import com.tsunami.authority.entity.auth.Menu;
import com.tsunami.base.R;
import com.tsunami.base.entity.SuperEntity;
import com.tsunami.log.annotation.SysLog;
import com.tsunami.user.annotation.LoginUser;
import com.tsunami.user.model.SysUser;
import com.tsunami.utils.TreeUtil2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname MenuController
 * @Description TODO
 * @Date 2021-10-29 13:45
 * @Created by lx
 */
@RestController
@RequestMapping("/menu")
@Api(value = "Menu", tags = "菜单管理")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    @GetMapping("list")
    @ApiOperation(notes = "菜单列表（分页）",value = "获取菜单列表")
    @SysLog("获取菜单列表")
    public R list(Integer pageNum,
                  Integer pageSize,
                  String orderByColumn,
                  String isAsc,
                  Menu data){
        return menuService.selectPageList(data);
    }

    @GetMapping("get")
    @ApiOperation(notes = "查询菜单",value = "查询菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单ID", dataType = "Long", paramType = "query", defaultValue = ""),
    })
    @SysLog("查询菜单")
    public R get(Long id){
        return R.ok().only(menuService.getById(id));
    }

    @ApiOperation(value = "新增菜单", notes = "新增菜单不为空的字段")
    @PostMapping("save")
    @SysLog("新增菜单")
    public R save(@LoginUser SysUser user, @Validated MenuSaveDTO data){
        Menu menu = new Menu();
//        menu.setTenantId();
        menu.setName(data.getName());
        menu.setDescribe(data.getDescribe());
        menu.setSPublic(data.getSPublic());
        menu.setPath(data.getPath());
        menu.setComponent(data.getComponent());
        menu.setSEnable(data.getSEnable());
        menu.setSortValue(data.getSortValue());
        menu.setIcon(data.getIcon());
        menu.setGroup(data.getGroup());
        menu.setParentId(data.getParentId());
        menu.setCreateUser(user.getId());
        menuService.save(menu);
        return R.ok();
    }

    @ApiOperation(value = "修改菜单", notes = "修改菜单")
    @PutMapping("/update")
    @SysLog("修改菜单")
    public R update(@LoginUser SysUser user,@RequestBody @Validated(SuperEntity.Update.class) MenuUpdateDTO data) {
        menuService.updateMenu(user,data);
        return R.ok();
    }

    @ApiOperation(value = "删除菜单", notes = "根据id物理删除菜单")
    @DeleteMapping("/delete")
    @SysLog("删除菜单")
    public R delete(@RequestParam("ids[]") List<Long> ids) {
        menuService.removeByIds(ids);
        return R.ok();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "group", value = "菜单组", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "long", paramType = "query"),
    })
    @ApiOperation(value = "查询用户可用的所有菜单", notes = "查询用户可用的所有菜单")
    @GetMapping("/menus")
    public R menus(@LoginUser SysUser user,
                   @RequestParam(value = "group", required = false) String group,
                   @RequestParam(value = "userId", required = false) Long userId,
                   @RequestParam(value = "code", required = false) String code) {
        if (userId == null || userId <= 0) {
            userId = user.getId();
        }
        List<Menu> list = menuService.findVisibleMenu(group, userId,code);
        List<MenuTreeDTO> treeList = new ArrayList<>();
        if(list!=null&&list.size()>0){
            for(Menu m:list){
                MenuTreeDTO menuTreeDTO = new MenuTreeDTO();
//                menuTreeDTO.setTenantId();
                menuTreeDTO.setName(m.getName());
                menuTreeDTO.setDescribe(m.getDescribe());
                menuTreeDTO.setSPublic(m.getSPublic());
                menuTreeDTO.setPath(m.getPath());
                menuTreeDTO.setComponent(m.getComponent());
                menuTreeDTO.setSEnable(m.getSEnable());
                menuTreeDTO.setSortValue(m.getSortValue());
                menuTreeDTO.setIcon(m.getIcon());
                menuTreeDTO.setGroup(m.getGroup());
                menuTreeDTO.setParentId(m.getParentId());
                menuTreeDTO.setId(m.getId());
                treeList.add(menuTreeDTO);
            }
        }
        List<MenuTreeDTO> tree = TreeUtil2.build(treeList);
        return R.ok().data(tree);
    }

    @ApiOperation(value = "查询系统所有的菜单", notes = "查询系统所有的菜单")
    @GetMapping("/tree")
    @SysLog("查询系统所有的菜单")
    public R allTree() {
        QueryWrapper param = new QueryWrapper();
        param.orderByAsc("sort_value");
        List<Menu> list = menuService.list(param);
        List<MenuTreeDTO> treeList = new ArrayList<>();
        if(list!=null&&list.size()>0){
            for(Menu m:list){
                MenuTreeDTO menuTreeDTO = new MenuTreeDTO();
//                menuTreeDTO.setTenantId();
                menuTreeDTO.setKey(m.getId());
                menuTreeDTO.setTitle(m.getName());
                menuTreeDTO.setName(m.getName());
                menuTreeDTO.setDescribe(m.getDescribe());
                menuTreeDTO.setSPublic(m.getSPublic());
                menuTreeDTO.setPath(m.getPath());
                menuTreeDTO.setComponent(m.getComponent());
                menuTreeDTO.setSEnable(m.getSEnable());
                menuTreeDTO.setSortValue(m.getSortValue());
                menuTreeDTO.setIcon(m.getIcon());
                menuTreeDTO.setGroup(m.getGroup());
                menuTreeDTO.setParentId(m.getParentId());
                menuTreeDTO.setId(m.getId());
                treeList.add(menuTreeDTO);
            }
        }
        List<MenuTreeDTO> tree = TreeUtil2.build(treeList);
        return R.ok().data(tree);
    }
}
